//
//  ZBRichViewController.m
//  XZBProduct
//
//  Created by xzb on 2018/8/8.
//  Copyright © 2018年 xzb. All rights reserved.
//

#import "ZBRichViewController.h"
#import "ZBRichScrollView.h"
#import <ZBFancyTableView/UITableView+ZBFancyTableView.h>
#import "ZBRichCell.h"
#import <KVOController/KVOController.h>

@interface ZBRichViewController ()

@property (nonatomic, strong) ZBRichScrollView *richView;
@property (nonatomic, strong) UIScrollView *contentScrollView;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation ZBRichViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"rich";
    
    self.richView = [[ZBRichScrollView alloc] init];
    self.richView.contentSize = CGSizeMake(0, [UIScreen height] * 3);
    [self.view addSubview:self.richView];
    [self.richView mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0, *)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(44);
        } else {
            make.top.offset(44);
        }
        make.left.right.bottom.offset(0);
    }];
    
    NSMutableArray *subView = @[].mutableCopy;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 100 , [UIScreen width], 44)];
    view.backgroundColor = [UIColor redColor];
    [self.richView addSubview:view];
    [subView addObject:view];
    
    self.contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(view.frame), [UIScreen width], 300)];
    self.contentScrollView.backgroundColor = [UIColor blackColor];
    [self.richView addSubview:self.contentScrollView];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.contentScrollView.width, self.contentScrollView.height)];
    self.tableView.backgroundColor = [UIColor cyanColor];
    [self.contentScrollView addSubview:self.tableView];
    [self.tableView zb_configTableView:^(ZBTableViewProtoFactory *config) {
        config.cell(@"cell").cls(@"ZBRichCell");
    }];
    [self.tableView zb_setup:^(ZBTableViewMaker *maker) {
        maker.section(@"section");
   
        for (NSInteger idx = 0; idx < 20; idx ++ ) {
            ZBRichCellModel *model = [ZBRichCellModel cellModel];
            model.indexPath = [NSIndexPath indexPathForRow:idx inSection:0];
            model.text = [NSString stringWithFormat:@"%ld",model.indexPath.row];
            maker.row(@"cell")
            .model(model);
        }
    }];
    self.richView.targetViews = subView;
    __weak typeof(self) weakSelf = self;
    [self.KVOController observe:self.tableView keyPath:@"contentOffset" options:NSKeyValueObservingOptionNew block:^(id  _Nullable observer, id  _Nonnull object, NSDictionary<NSString *,id> * _Nonnull change) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        [strongSelf handleTableView];
    }];
}
- (void)handleTableView
{
    NSLog(@"%@",NSStringFromCGPoint(self.tableView.contentOffset));
    
    CGFloat offsetY = self.tableView.contentOffset.y;
    
    CGRect tmpRect = self.contentScrollView.frame;
    tmpRect.origin.y -= offsetY;
    self.contentScrollView.frame = tmpRect;
    
}
@end
